<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Row\Allot;
use App\Admin\Actions\Show\Audit;
use App\Models\MaintainOrder;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Storage;

class MaintainOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(MaintainOrder::with(['applyuser','maintainuser','audituser']), function (Grid $grid) {
            //$grid->column('id');
            $grid->column('created_at','报修日期')->display(function ($v){
                return date('Y-m-d',strtotime($v));
            });
            $grid->column('order_name');
            $grid->column('order_status')->using(MaintainOrder::ORDER_STATUS)->badge([
                0 =>  'red',
                1 =>  'yellow',
                2 =>  'blue',
                3 =>  'gray',
            ]);
            $grid->column('maintain_site');
            $grid->column('apply_content');
            $grid->column('applyuser.name');
            $grid->column('apply_file')->display(function ($value){
                $data = explode('|', $value);
                $str_img = '';
                foreach ($data as $v) {
                    $str_img .= '<img style="width:20px;margin:5px;" src="' . $v . '"/>';
                }
                return $str_img;
            });

            $grid->column('maintainuser.name');
            $grid->column('is_replace_maintain')->using(MaintainOrder::IS_REPLACE_MAINTAIN)->badge([
                0 =>  'green',
                1 =>  'red',
            ]);
            $grid->column('maintain_money');
            $grid->column('mounting_money');
            $grid->column('sum_money');
            $grid->column('maintain_time')->display(function ($v){
                return empty($v) ? '' : date('Y-m-d',strtotime($v));
            });
            $grid->column('maintain_content');
            $grid->column('maintain_images')->display(function ($value){
                $data = explode('|', $value);
                $str_img = '';
                foreach ($data as $v) {
                    $str_img .= '<img style="width:20px;margin:5px;" src="' . $v . '"/>';
                }
                return $str_img;
            });

            $grid->column('audituser.name');
            $grid->column('audit_content');
            $grid->column('audit_time')->display(function ($v){
                return empty($v) ? '' : date('Y-m-d',strtotime($v));
            });

            $grid->model()->orderBy('id','desc');
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableQuickEditButton();

            $grid->export()->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    $row['order_status']        = MaintainOrder::ORDER_STATUS[$row['order_status']];
                    $row['is_replace_maintain'] = MaintainOrder::IS_REPLACE_MAINTAIN[$row['is_replace_maintain']];
                    unset($row['apply_file']);
                    unset($row['maintain_images']);
                }

                return $rows;
            });

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableQuickEdit();
                $actions->append(new Allot());
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('order_status', '状态', MaintainOrder::ORDER_STATUS);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->between('created_at','报修日期')->date();
                $filter->like('order_name');
                $filter->equal('apply_user')->select(User::query()->pluck('name','id'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, MaintainOrder::with(['applyuser','maintainuser','audituser']), function (Show $show) {
            $show->html(view('admin/maintain_order_details',['data'=>$show->model()]));
            $show->disableEditButton();
            $show->tools(new Audit());
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MaintainOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_name');
            $form->text('apply_file');
            $form->text('apply_content');
            $form->text('maintain_user');
            $form->text('maintain_content');
            $form->text('audit_content');
            $form->text('order_status');
            $form->text('is_replace_maintain');
            $form->text('maintain_money');
            $form->text('mounting_money');
            $form->text('sum_money');

        });
    }
}
